package com.itheima.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.itheima.user.config.JwtProperties;
import com.itheima.user.domain.po.User;
import com.itheima.user.mapper.UserMapper;
import com.itheima.user.service.IUserService;
import com.itheima.user.utils.JwtTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserserviceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private  JwtTool jwtTool;
    @Autowired
    private  JwtProperties jwtProperties;


    @Override
    public String login(String phone, String pwd) {
        //校验格式
        if (StrUtil.isEmpty(phone)||StrUtil.isEmpty(pwd)){
            return "手机号或密码不能为空";
        }
        //校验手机号密码
        User user =  userMapper.selectOne(phone,pwd);

        if (user==null){
            return "手机号或密码错误";
        }

        // 5.生成TOKEN
        String token = jwtTool.createToken(user.getId(), jwtProperties.getTokenTTL());
        return token;
    }

    @Override
    public String insert(User user) {
       int i = userMapper.insert(user);
        return i > 0 ? "注册成功" : "注册失败";
    }

    @Override
    public String deleteById(Long id) {
      int i  =  userMapper.deleteById(id);
        return i > 0 ? "删除成功" : "删除失败";
    }

    @Override
    public String updateById(User user) {
       int i =  userMapper.updateById(user);
        return i > 0 ? "更新成功" : "更新失败";
    }

    @Override
    public User getById(Long id) {
        User  user = userMapper.selectById(id);
        return user;
    }
}
